﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Toolkit {

  public static class GraphicTools {

    public static void DrawRoundedRectangle( System.Drawing.Graphics graphics, Pen pen, int x, int y, int width, int height, int radius ) {

      GraphicsPath gp = new GraphicsPath( );

      gp.AddLine( x + radius, y, x + width - ( radius * 2 ), y ); // Line
      gp.AddArc( x + width - ( radius * 2 ), y, radius * 2, radius * 2, 270, 90 ); // Corner
      gp.AddLine( x + width, y + radius, x + width, y + height - ( radius * 2 ) ); // Line
      gp.AddArc( x + width - ( radius * 2 ), y + height - ( radius * 2 ), radius * 2, radius * 2, 0, 90 ); // Corner
      gp.AddLine( x + width - ( radius * 2 ), y + height, x + radius, y + height ); // Line
      gp.AddArc( x, y + height - ( radius * 2 ), radius * 2, radius * 2, 90, 90 ); // Corner
      gp.AddLine( x, y + height - ( radius * 2 ), x, y + radius ); // Line
      gp.AddArc( x, y, radius * 2, radius * 2, 180, 90 ); // Corner

      gp.CloseFigure( );
      graphics.DrawPath( pen, gp );

      gp.Dispose( );
    }

    public static class Convert {

      public static byte[ ] ImageToByteArray( Image image, ImageFormat imageFormat ) {
        byte[ ] value;
        Bitmap temp = new Bitmap( image );
        try {
          using ( MemoryStream memoryStream = new MemoryStream( ) ) {
            temp.Save( memoryStream, imageFormat );
            value = memoryStream.ToArray( );
          }
        } catch ( Exception ex ) {
          throw new Exception( ex.Message );
        }

        return value;
      }

      public static Image ByteArrayToImage( byte[ ] imageArray ) {
        Image image = null;
        try {
          using ( MemoryStream ms = new MemoryStream( imageArray, 0, imageArray.Length ) ) {
            ms.Write( imageArray, 0, imageArray.Length );
            image = Image.FromStream( ms, true );
          }
        } catch ( Exception ex ) {
          throw new Exception( ex.Message );
        }

        return image;
      }
   
    }

  }
}
